package com.zmn.oms.admin.controller.work.list;

import com.zmn.common.dto2.data.LayuiTableDO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.consts.GlobalConsts;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.model.entity.work.OrderWorkQuery;
import com.zmn.oms.model.vo.work.list.ZsOrderWorkHqFinanceVO;
import com.zmn.oms.zmn.business.interfaces.work.list.ZsOrderWorkHqFinanceQueryBService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;

/**
 * 类描述：总部财务工单列表
 *
 * @author liuying
 * @date 2018/12/27 12:04
 */
@Controller
@RequestMapping("/hqfinance/work")
public class WorkHqFinanceListController extends WorkListTabBaseController {

    @Autowired
    ZsOrderWorkHqFinanceQueryBService zsOrderWorkHqFinanceQueryBService;

    /**
     * 工单列表
     */
    @RequiresPermissions("/zs/hqfinance/work/list.action")
    @RequestMapping(value = "/list")
    public ModelAndView list(OrderWorkQuery query, ModelAndView mv) {
        mv.setViewName("work/list/hqfinance/list");

        // 默认日期
        mv.addObject("startTime", DateUtil.toString(DateUtil.getNow(), "yyyy-MM-01 00:00:00"));
        mv.addObject("endTime", DateUtil.toString(DateUtil.getNow(), "yyyy-MM-dd 23:59:59"));

        super.setDayLimit(mv);
        super.setDefaultModelAndView(mv);

        // 默认查询条件
        mv.addObject("query", query);
        return mv;
    }

    /**
     * 工单列表 ajax
     */
    @RequiresPermissions("/zs/hqfinance/work/list.action")
    @PostMapping(value = "/ajax/list")
    @ResponseBody
    public ResultDTO ajaxList(@RequestBody OrderWorkQuery query) {
        // 处理查询条件
        processSearchQuery(query);

        List<ZsOrderWorkHqFinanceVO> voList = zsOrderWorkHqFinanceQueryBService.listPageOrderWorkByQuery(query);
        LayuiTableDO<ZsOrderWorkHqFinanceVO> data = new LayuiTableDO<>();
        data.setCount(query.getTotalCount());
        data.setData(voList);
        return ResultDTO.success(data);
    }

    /**
     * 工单,单条 ajax
     */
    @RequiresPermissions("/zs/hqfinance/work/list.action")
    @GetMapping(value = "/ajax/row")
    @ResponseBody
    public ResultDTO ajaxRow(@Valid OrderWorkQuery query) {
        // 处理查询条件
        processSearchQuery(query);
        List<ZsOrderWorkHqFinanceVO> voList = zsOrderWorkHqFinanceQueryBService.listPageOrderWorkByQueryNoRecord(query);
        return ResultDTO.success(voList.get(0));
    }

    /**
     * 处理查询条件
     *
     * @param query
     */
    private void processSearchQuery(OrderWorkQuery query) {
        // 脱敏
       // query.setIsSensitiveAddr(!StaffAdminService.isPermitted(OrderPremissionConsts.getOrderWorkListAddressPermitUrl(OrderPremissionConsts.MODULE_ID_ZS_HQFINANCE)));
        query.setIsSensitiveAddr(true);
        query.setIsSensitiveTel(true);

        // 查询服务平台是啄木鸟的
        query.setPlatWork(GlobalConsts.PLAT_MARK_ZMN);

        // 查询手机号对应的用户
        query.setQueryConcatPhoneUsers(true);
    }

}